package com.yoomiito.app.ui.fragment.my;

import android.os.Bundle;
import android.view.View;

import com.yoomiito.app.Constant;
import com.yoomiito.app.adapter.my.MyOrderAdapter;
import com.yoomiito.app.model.my.MyOrderBean;
import com.yoomiito.app.model.my.MyTodayOrderInfo;
import com.yoomiito.app.present.my.fragment.MyOrderFmPresenter;
import com.yoomiito.app.ui.my.MyTodayOrderActivity;
import com.yoomiito.app.utils.L;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 今日订单界面
 */
public class MyOrderFragment extends MyOrderBaseFragment<MyOrderFmPresenter> {
    public static final String MyOrderFm = "MyOrderFm";

//    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private MyOrderAdapter mAdapter;
    private List<MyOrderBean> mList;
    private int type;

    @Override
    public MyOrderFmPresenter newP() {
        return new MyOrderFmPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(Constant.KEY_TYPE, Constant.NAMAL_TYPE_TAOBAO);
        }
        mList = new ArrayList<>();
        mAdapter = new MyOrderAdapter(mList, type, MyOrderFm);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            showLoading();
            getP().getTip(view,mAdapter.getData().get(position).getOrder_id());
        });
//        getNewData();
    }

    @Override
    protected void getNewData() {
        getP().getTodayOrderData(currentPage, type);
    }

    @Override
    public String getEmptyContent() {
        return "未购买商品";
    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setTodayOrderData(MyTodayOrderInfo data) {
        ((MyTodayOrderActivity)context).moneyTv.setText(data.getTodayIncomeTotal());
        if (currentPage == 1) {
            mSmartRefreshLayout.finishRefresh(true);
            if (data != null && data.getData() != null&&data.getData().size()>0) {
                mAdapter.setNewData(data.getData());
                mStatusManager.showContent();
            }else {
                mStatusManager.showEmpty();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data.getData() == null || data.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data.getData());
            }
        }
    }

    public void setTip(View view,List<String> tips){
        dismissLoading();
        String tip = "";
        if (tips!=null&&tips.size()>0){
            for (int i = 0; i < tips.size(); i++) {
                if (i==0){
                    tip = tips.get(0);
                }else {
                    tip = tip+"\n"+tips.get(i);
                }
            }
            showTip(view,tip,2);
        }
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }
}
